package com.weimob.demo.configuration;

import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.filter.logging.LogFilter;
import com.alibaba.druid.filter.logging.Slf4jLogFilter;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

/**
 * Created by junxiao on 2016/12/31.
 */
@Configuration
public class DruidConfiguration {

    @Value("${spring.datasource.type}")
	private Class<? extends DataSource> datasourceType;

	@Bean(name = "${spring.mysqlDS}")
    @ConfigurationProperties(prefix = "spring.datasource.mysql")
    public DataSource mysqlDataSource() {
        return getDataSource();
    }

    @Bean(name = "mysqlTransactionManager")
    public DataSourceTransactionManager mysqlTransactionManager(@Qualifier("${spring.mysqlDS}") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "mysqlSqlSessionFactory")
    public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("${spring.mysqlDS}") DataSource dataSource) throws Exception {
        return getSqlSessionFactory(dataSource);
    }

    @Primary
    @Bean(name = "${spring.sqlserverDS}")
    @ConfigurationProperties(prefix = "spring.datasource.sqlserver")
    public DataSource sqlserverDataSource() {
        return getDataSource();
    }

    @Primary
    @Bean(name = "sqlserverTransactionManager")
    public DataSourceTransactionManager sqlserverTransactionManager(@Qualifier("${spring.sqlserverDS}") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Primary
    @Bean(name = "sqlserverSqlSessionFactory")
    public SqlSessionFactory sqlserverSqlSessionFactory(@Qualifier("${spring.sqlserverDS}") DataSource dataSource) throws Exception {
        return getSqlSessionFactory(dataSource);
    }

    private DataSource getDataSource() {
        DruidDataSource dataSource = (DruidDataSource) DataSourceBuilder.create().type(datasourceType).build();
        List<Filter> lisFilter =  new ArrayList<Filter>();
        lisFilter.add(slf4jFilter());
        dataSource.setProxyFilters(lisFilter);
        return dataSource;
    }

    private SqlSessionFactory getSqlSessionFactory(DataSource dataSource) throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
//        sessionFactory.setMapperLocations();
//        sessionFactory.setTypeAliasesPackage();
//        sessionFactory.setConfigLocation();
        return sessionFactory.getObject();
    }
	

    public LogFilter slf4jFilter() {
    	LogFilter filter = new Slf4jLogFilter();
        filter.setStatementExecutableSqlLogEnable(true);
        return filter;
    }
}
